
randomsquare={...
    'chapter4',...
    'chapter5b',...
    '2010-11-16_14_7_17-Square-Random-20x20',...
    '2010-11-18_9_15_24-Square-Random-20x20',...
    '2010-11-18_9_31_50-Square-Random-20x20',...
    '2010-11-18_9_48_47-Square-Random-20x20',...
    '2010-11-18_10_7_43-Square-Random-20x20',...
    '2010-11-18_10_25_32-Square-Random-20x20',...
    '2010-11-21_15_53_39-Square-Random-20x20',...
    '2010-11-21_16_27_5-Square-Random-20x20',...
    '2010-11-21_16_46_20-Square-Random-20x20',...
    '2010-11-21_17_18_18-Square-Random-20x20',...
    '2010-11-21_17_1_10-Square-Random-20x20',...
    '2010-11-21_17_33_31-Square-Random-20x20',...
    '2010-11-21_17_48_53-Square-Random-20x20',...
    '2010-11-21_18_18_52-Square-Random-20x20',...
    '2010-11-21_18_35_14-Square-Random-20x20',...
    '2010-11-21_18_3_42-Square-Random-20x20',...
    '2010-11-21_18_50_45-Square-Random-20x20',...
    '2010-11-21_19_21_10-Square-Random-20x20',...
    '2010-11-21_19_36_39-Square-Random-20x20',...
    '2010-11-21_19_52_52-Square-Random-20x20',...
    '2010-11-21_19_6_2-Square-Random-20x20',...
    '2010-11-21_20_23_49-Square-Random-20x20',...
    '2010-11-21_20_38_54-Square-Random-20x20',...
    '2010-11-21_20_8_21-Square-Random-20x20',...
    '2010-11-21_21_39_53-Square-Random-20x20'
    };

cfolders={...
    '2010-11-18_10_52_54-C-Random-20x20',...
    '2010-11-18_11_39_42-C-Random-20x20',...
    '2010-11-18_11_56_23-C-Random-20x20',...
    '2010-11-18_12_13_11-C-Random-20x20',...
    '2010-11-18_12_30_6-C-Random-20x20',...
    '2010-11-18_12_46_25-C-Random-20x20',...
    '2010-11-18_13_19_56-C-Random-20x20',...
    '2010-11-18_13_2_35-C-Random-20x20',...
    '2010-11-18_13_37_11-C-Random-20x20'%,...
%     '2010-11-21_21_44_47-C-Random-20x20'
    };

pipes_20x2={...
    '2010-11-23_20_16_5-Rectangle-Random-20x2',...
    '2010-11-23_20_47_32-Rectangle-Random-20x2',...
    '2010-11-23_22_37_54-Rectangle-Random-20x2',...
    '2010-11-23_22_6_17-Rectangle-Random-20x2',...
    '2010-11-24_20_27_30-Rectangle-Random-20x2',...
    '2010-11-24_20_2_31-Rectangle-Random-20x2'
    };
pipes_20x4={...
    '2010-11-24_21_4_19-Rectangle-Random-20x4',...
    '2010-11-24_21_54_39-Rectangle-Random-20x4',...
    '2010-11-24_22_43_42-Rectangle-Random-20x4',...
    '2010-11-24_23_32_19-Rectangle-Random-20x4',...
    '2010-11-25_0_21_42-Rectangle-Random-20x4',...
    '2010-11-25_1_10_40-Rectangle-Random-20x4'...
    };

pipes_25x1={...
    '2010-12-19_21_43_33-Rectangle-Random-1x25',...
    '2010-12-19_21_52_25-Rectangle-Random-1x25',...
    '2010-12-19_21_43_33-Rectangle-Random-1x25',...
    '2010-12-19_21_52_25-Rectangle-Random-1x25',...
    '2010-12-19_22_1_9-Rectangle-Random-1x25',...
    '2010-12-19_22_9_24-Rectangle-Random-1x25'
    };
pipes_25x2={...
    '2010-12-19_21_15_44-Rectangle-Random-2x25',...
    '2010-12-19_21_23_35-Rectangle-Random-2x25',...
    '2010-12-19_21_31_52-Rectangle-Random-2x25',...
    '2010-12-19_21_40_13-Rectangle-Random-2x25',...
    '2010-12-19_21_48_18-Rectangle-Random-2x25',...
    '2010-12-19_21_56_37-Rectangle-Random-2x25',...
    '2010-12-19_21_7_33-Rectangle-Random-2x25'
    };
pipes_25x3={...
    '2010-12-18_15_47_47-Rectangle-Random-25x3',...
    '2010-12-19_10_20_27-Rectangle-Random-25x3',...
    '2010-12-19_21_14_18-Rectangle-Random-25x3',...
    '2010-12-19_21_21_54-Rectangle-Random-25x3',...
    '2010-12-19_21_29_23-Rectangle-Random-25x3',...
    '2010-12-19_21_37_0-Rectangle-Random-25x3',...
    '2010-12-19_21_44_26-Rectangle-Random-25x3',...
    '2010-12-19_21_52_33-Rectangle-Random-25x3',...
    '2010-12-19_21_6_47-Rectangle-Random-25x3'
    };
pipes_25x4={...
    '2010-12-19_10_18_35-Rectangle-Random-25x4',...
    '2010-12-19_10_26_8-Rectangle-Random-25x4',...
    '2010-12-19_10_34_6-Rectangle-Random-25x4',...
    '2010-12-19_10_42_10-Rectangle-Random-25x4',...
    '2010-12-19_10_50_2-Rectangle-Random-25x4',...
    '2010-12-19_10_58_5-Rectangle-Random-25x4',...
    '2010-12-19_11_12_30-Rectangle-Random-25x4',...
    '2010-12-19_11_19_52-Rectangle-Random-25x4',...
    '2010-12-19_11_27_35-Rectangle-Random-25x4',...
    '2010-12-19_11_34_55-Rectangle-Random-25x4',...
    '2010-12-19_11_41_57-Rectangle-Random-25x4',...
    '2010-12-19_11_49_3-Rectangle-Random-25x4',...
    '2010-12-19_11_56_6-Rectangle-Random-25x4',...
    '2010-12-19_11_5_15-Rectangle-Random-25x4',...
    '2010-12-19_12_11_46-Rectangle-Random-25x4',...
    '2010-12-19_12_18_42-Rectangle-Random-25x4',...
    '2010-12-19_12_26_2-Rectangle-Random-25x4',...
    '2010-12-19_12_33_17-Rectangle-Random-25x4',...
    '2010-12-19_12_3_12-Rectangle-Random-25x4',...
    '2010-12-19_12_40_30-Rectangle-Random-25x4',...
    '2010-12-1_0_2_13-Rectangle-Random-25x4'
    };
folders={pipes_25x1,pipes_25x2,pipes_25x3,pipes_25x4};
label='pipes';
names={'Pipeline 25x1','Pipeline 25x2','Pipeline 25x3','Pipeline 25x4'};
   
% folders={cfolders};
% label='cshapes';
% names={'C-Shape 20x20'};

numsets=length(folders);
allErrors=cell(numsets,1);
minHeights=cell(numsets,1);
sumDistances=cell(numsets,1);
meanDistances=cell(numsets,1);
medianDistances=cell(numsets,1);
areas=cell(numsets,1);
connectivityLevels=zeros(numsets,1);
for f=1:length(folders)
    allErrors{f}=zeros(0,1);
    minHeights{f}=zeros(0,1);
    sumDistances{f}=zeros(0,1);
    meanDistances{f}=zeros(0,1);
    medianDistances{f}=zeros(0,1);
    areas{f}=zeros(0,1);
end
for f=1:length(folders)
    cl=zeros(length(folders{f}));
    for i=1:length(folders{f})
        folder=sprintf('../results/%s',folders{f}{i});
        vars=loadFile(folder,'errorsAndAnchors');
        if (isempty(vars))
            vars=loadFile(folder,'result');
            if ~isfield(vars,'result')
                fprintf(2,'Skipping %s',folder);
                continue;
            end
            result=vars.result;
            errors=result.errors;
            network=result.network;
            radius=result.radius;
            vars=loadFile(folder,'anchors');
            anchors=vars.anchors;
            
            savefile=sprintf('%s/errorsAndAnchors.mat',folder);
            save(savefile,'errors','anchors','network','radius');
            clear result
        else
            errors=vars.errors;
            network=vars.network;
            anchors=vars.anchors;
            if (isfield(vars,'radius'))
                radius=vars.radius;
            else
                radius=2.5;
            end
        end
        allErrors{f}=[ allErrors{f} [errors.mean] ]; %#ok grow
        stats=triangleStats(network.points,anchors,network.width,network.height);
        minHeights{f}=[ minHeights{f} [stats.heights.min] ]; %#ok grow
        areas{f}=[ areas{f} stats.areas' ]; %#ok grow
        sums=zeros(1,size(anchors,1));
        means=zeros(1,size(anchors,1));
        medians=zeros(1,size(anchors,1));
        
        cl(i)=network.networkConnectivityLevel;
        
        d=zeros(1,3);
        for s=1:size(anchors,1)
            anchorNodes=anchors(s,:);
            d(1)=network.distanceMatrix(anchorNodes(1),anchorNodes(2));
            d(2)=network.distanceMatrix(anchorNodes(2),anchorNodes(3));
            d(3)=network.distanceMatrix(anchorNodes(3),anchorNodes(1));
            sums(s)=sum(d);
            medians(s)=median(d);
            means(s)=mean(d);
        end
        sumDistances{f}=[ sumDistances{f} sums  ]; %#ok grow
        meanDistances{f}=[ meanDistances{f} means  ]; %#ok grow
        medianDistances{f}=[ medianDistances{f} medians ]; %#ok grow
        
        clear network errors anchors numAnchorSets
    end
    connectivityLevels(f)=mean(cl(1));
end
subtitle=sprintf('(factor of radio radius)');
h=plotIndicators(allErrors,minHeights,radius,{'Minimum Anchor Triangle Height',subtitle},0.1,false,'log',names,connectivityLevels);
ylim([0 5]);
filename=sprintf('HeightIndicator_%s',label);
saveFigure('..',filename,h);
hold off

h=plotIndicators(allErrors,areas,radius,{'Anchor Triangle Area',subtitle},0.1,true,'log',names,connectivityLevels);
filename=sprintf('AreaIndicator_%s',label);
saveFigure('..',filename,h);
hold off

% outliers=find(allErrors>2.5);
% allErrors(outliers)=[];
% sumDistances(outliers)=[];

h=plotIndicators(allErrors,sumDistances,radius,{'Sum of Distance between Anchors',subtitle},2,false,'linear',names,connectivityLevels);
filename=sprintf('SumOfDistanceIndicator_%s',label);
saveFigure('..',filename,h);
hold off

h=plotIndicators(allErrors,medianDistances,radius,{'Median of Distance between Anchors',subtitle},2,false,'linear',names,connectivityLevels);
filename=sprintf('MedianOfDistanceIndicator_%s',label);
saveFigure('..',filename,h);
hold off

h=plotIndicators(allErrors,meanDistances,radius,{'Mean of Distance between Anchors',subtitle},2,false,'linear',names,connectivityLevels);
filename=sprintf('MeanOfDistanceIndicator_%s',label);
saveFigure('..',filename,h);
hold off
